package gaej.example.contact.client;

import java.util.List;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.HTMLTable.Cell;

public class DirigentGUI {
    /* Constants. */
    private static final String LISTING_ROOT_PANEL = "guiListing";
    private static final String FORM_ROOT_PANEL = "guiForm";
    private static final String STATUS_ROOT_PANEL = "guiStatus";
    private static final String TOOL_BAR_ROOT_PANEL = "guiToolBar";
    private static final int EDIT_LINK = 3;
    private static final int REMOVE_LINK = 4;

    /* GUI Widgets */
    protected Button addButton;
    protected Button updateButton;
    protected Button addNewButton;
    protected TextBox naamField;
    protected TextBox landField;
    protected TextBox richtingField;
    protected TextBox klasseField;
    protected Label status;
    protected Grid listGrid;
    protected Grid formGrid;
    
    /* Data model */
    private List<Dirigent> dirigenten;
    private Dirigent currentDirigent;
    private Persoon currentPersoon;
    protected DirigentDelegate dirigentService;
        
    public void init() {
        addButton = new Button("Add dirigent");
        addNewButton = new Button("Add new dirigent");
        updateButton = new Button("Update dirigent");
        naamField = new TextBox();
        landField = new TextBox();
        richtingField = new TextBox();
        klasseField = new TextBox();
        status = new Label();
        listGrid = new Grid(2,6);

        buildForm();
        placeWidgets();
    }
    
    private void buildForm() {
        formGrid = new Grid(5,3);
        formGrid.setVisible(false);
        
        formGrid.setWidget(0, 0, new Label("Naam"));
        formGrid.setWidget(0, 1, naamField);

        formGrid.setWidget(1, 0, new Label("Land"));
        formGrid.setWidget(1, 1, landField);
        
        formGrid.setWidget(2, 0, new Label("Richting"));
        formGrid.setWidget(2, 1, richtingField);

        formGrid.setWidget(3, 0, new Label("Klasse"));
        formGrid.setWidget(3, 1, klasseField);
        
        formGrid.setWidget(4, 0, updateButton);
        formGrid.setWidget(4, 1, addButton);
        
    }

    private void placeWidgets() {
        RootPanel.get(LISTING_ROOT_PANEL).add(listGrid);
        RootPanel.get(FORM_ROOT_PANEL).add(formGrid);
        RootPanel.get(STATUS_ROOT_PANEL).add(status);
        RootPanel.get(TOOL_BAR_ROOT_PANEL).add(addNewButton);
    }

    public void hideGui() {
    	formGrid.setVisible(false);
    	listGrid.setVisible(false);
    	status.setVisible(false);
    	addNewButton.setVisible(false);
    }

    
    private void loadForm(Dirigent dirigent) {
        this.formGrid.setVisible(true);
        currentDirigent = dirigent;
        currentPersoon = dirigent.getPersoon();
        this.naamField.setText(currentPersoon.getNaam());
        this.landField.setText(currentPersoon.getLand());
        this.richtingField.setText(dirigent.getRichting());
        this.klasseField.setText(dirigent.getKlasse());
    }


    private void copyFieldDateToDirigent() {
        currentPersoon.setNaam(naamField.getText());
        currentPersoon.setLand(landField.getText());
        currentDirigent.setPersoon(currentPersoon);
        currentDirigent.setRichting(richtingField.getText());
        currentDirigent.setKlasse(klasseField.getText());
    }

    public void gui_eventListGridClicked(Cell cellClicked) {
         int row = cellClicked.getRowIndex();
         int col = cellClicked.getCellIndex();
        
         Dirigent dirigent = this.dirigenten.get(row);
         Persoon persoon = dirigent.getPersoon();
         this.status.setText("Naam was " + persoon.getNaam() + " clicked ");
        
         if (col==EDIT_LINK) {
             this.addNewButton.setVisible(false);
             this.updateButton.setVisible(true);
             this.addButton.setVisible(false);
             loadForm(dirigent);
         } else if (col==REMOVE_LINK) {
        	 if (Window.confirm("Verwijderen dirigent ?")) {
        		 this.dirigentService.removeDirigent(dirigent);
        	 }
         }
    }


    public void gui_eventAddButtonClicked() {
        addNewButton.setVisible(true);
        formGrid.setVisible(false);
        copyFieldDateToDirigent();
        this.dirigentService.addDirigent(currentDirigent);
    }

    public void gui_eventUpdateButtonClicked() {
        addNewButton.setVisible(true);
        formGrid.setVisible(false);
        copyFieldDateToDirigent();
        this.dirigentService.updateDirigent(currentDirigent);
    }

    public void gui_eventAddNewButtonClicked() {
        this.addNewButton.setVisible(false);
        this.updateButton.setVisible(false);
        this.addButton.setVisible(true);
        Dirigent dirigent = new Dirigent();
        Persoon persoon = new Persoon();
        dirigent.setPersoon(persoon);
        loadForm(dirigent);
    }


    public void service_eventListRetrievedFromService(List<Dirigent> result) {
        status.setText("Retrieved dirigent list");
        status.setVisible(true);

        //formGrid.setVisible(false);
    	listGrid.setVisible(true);
    	status.setVisible(true);
    	addNewButton.setVisible(true);
        
        this.dirigenten = result;
        this.listGrid.clear();
        this.listGrid.resizeRows(this.dirigenten.size());
        int row = 0;
        this.addNewButton.setVisible(true);
        
        for (Dirigent dirigent : result) {
        	Persoon persoon = dirigent.getPersoon();
        	if (persoon == null) {
        		persoon = new Persoon("--","--");
        	}
        	this.listGrid.setWidget(row, 0, new Label (persoon.getNaam()));
            this.listGrid.setWidget(row, 1, new Label (persoon.getLand()));
            this.listGrid.setWidget(row, 2, new Label (dirigent.getRichting()));
            this.listGrid.setWidget(row, 3, new Label (dirigent.getKlasse()));
            this.listGrid.setWidget(row, EDIT_LINK, new Hyperlink("Edit", null));
            this.listGrid.setWidget(row, REMOVE_LINK, new Hyperlink("Remove", null));
            row ++;
        }
    }

    public void service_eventAddDirigentSuccessful() {
        status.setText("Dirigent was successfully added");
        this.dirigentService.listDirigenten();
    }

    public void service_eventUpdateSuccessful() {
        status.setText("Dirigent was successfully updated");
        this.dirigentService.listDirigenten();
    }
    public void service_eventRemoveDirigentSuccessful() {
        status.setText("Dirigent was removed");
        this.dirigentService.listDirigenten();
    }

    public void service_eventUpdateDirigentFailed(Throwable caught) {
        status.setText("Update dirigent failed");
    }

    public void service_eventAddDirigentFailed(Throwable caught) {
        status.setText("Unable to update dirigent");
    }

    public void service_eventRemoveDirigentFailed(Throwable caught) {
        status.setText("Remove dirigent failed");
    }

    public void service_eventListDirigentenFailed(Throwable caught) {
        status.setText("Unable to get dirigent list");
        
    }

}
